package com.yanliang.client;

import com.yanliang.model.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * @author yanliang
 * @date 5/23/20202:53 PM
 */
public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

	private ChannelHandlerContext context;
	private String result;
	private RpcRequest rpcRequest;

	public void channelActive(ChannelHandlerContext ctx) {
		context = ctx;
	}

	/**
	 * 收到服务端数据，唤醒等待线程
	 * @param ctx
	 * @param msg
	 */
	public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) {
		result = msg.toString();
		notify();
	}

	/**
	 * 写出数据，开始等待唤醒
	 * @return
	 * @throws InterruptedException
	 */
	public synchronized Object call() throws InterruptedException {
		context.writeAndFlush(rpcRequest);
		wait();
		return result;
	}

//	/**
//	 * 设置参数
//	 * @param para
//	 */
//	void setPara(String para) {
//		this.para = para;
//	}

	/**
	 * 设置RpcRequest参数
	 * @param request
	 */
	void setRpcPara(RpcRequest request) {
		this.rpcRequest = request;
	}
}
